package com.jsh.jshManage.service.tenant;

import com.alibaba.fastjson.JSONObject;
import com.jsh.jshManage.datasource.entities.Tenant;
import com.jsh.jshManage.datasource.entities.TenantExample;
import com.jsh.jshManage.datasource.mappers.TenantMapper;
import com.jsh.jshManage.datasource.mappers.TenantMapperEx;
import com.jsh.jshManage.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class TenantService {
    private Logger logger = LoggerFactory.getLogger(TenantService.class);
    @Resource
    private TenantMapper tenantMapper;

    @Resource
    private TenantMapperEx tenantMapperEx;

    public Tenant getTenant(long id) {
        return tenantMapper.selectByPrimaryKey(id);
    }

    public List<Tenant> getTenant() {
        TenantExample example = new TenantExample();
        return tenantMapper.selectByExample(example);
    }

    public List<Tenant> select( String loginName, int offset, int rows) {
        return tenantMapperEx.selectByConditionTenant(loginName, offset, rows);
    }

    public Long countTenant(String loginName) {
        return tenantMapperEx.countsByTenant(loginName);
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertTenant(String beanJson, HttpServletRequest request) {
        Tenant tenant = JSONObject.parseObject(beanJson, Tenant.class);
        tenant.setUserNumLimit(2); //默认用户限制数量
        tenant.setBillsNumLimit(200); //默认单据限制数量
        return tenantMapper.insertSelective(tenant);
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateTenant(String beanJson, Long id) {
        Tenant tenant = JSONObject.parseObject(beanJson, Tenant.class);
        tenant.setId(id);
        return tenantMapper.updateByPrimaryKeySelective(tenant);
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteTenant(Long id) {
        return tenantMapper.deleteByPrimaryKey(id);
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteTenant(String ids) {
        List<Long> idList = StringUtil.strToLongList(ids);
        TenantExample example = new TenantExample();
        example.createCriteria().andIdIn(idList);
        return tenantMapper.deleteByExample(example);
    }

    public int checkIsNameExist(Long id, String name) {
        TenantExample example = new TenantExample();
        example.createCriteria().andIdEqualTo(id);
        List<Tenant> list = tenantMapper.selectByExample(example);
        return list.size();
    }

    public Tenant getTenantByTenantId(long tenantId) {
        Tenant tenant = new Tenant();
        TenantExample example = new TenantExample();
        example.createCriteria().andTenantIdEqualTo(tenantId);
        List<Tenant> list = tenantMapper.selectByExample(example);
        if(list.size()>0) {
            tenant = list.get(0);
        }
        return tenant;
    }
}
